package com.zhou.syt.hosp.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.atguigu.yygh.model.hosp.Hospital;
import com.atguigu.yygh.vo.hosp.HospitalQueryVo;
import com.zhou.syt.hosp.repository.HospRepostitory;
import com.zhou.syt.hosp.service.HospitalService;
import com.zhou.yygh.cmn.client.DictFeignClient;
import org.springframework.beans.BeanUtils;
import org.springframework.data.domain.*;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @ClassName: HospitalServiceImpl
 * @Description:
 * @Author: admin
 * @Date: 2022/1/7 10:52
 */
@Service
public class HospitalServiceImpl implements HospitalService {

    @Resource
    private HospRepostitory hospRepostitory;

    @Resource
    private DictFeignClient dictFeignClient;

    @Override
    public void save(Map<String, Object> stringObjectMap) {
        //把map集合转换为对象
        String s = JSONObject.toJSONString(stringObjectMap);
        Hospital hospital = JSONObject.parseObject(s, Hospital.class);
        //判断当前记录是否存在
        String hoscode = hospital.getHoscode();
        Hospital hospitalByHoscode = hospRepostitory.getHospitalByHoscode(hoscode);
        //存在就修改
        if (hospitalByHoscode != null) {
            hospital.setStatus(hospitalByHoscode.getStatus());
            hospital.setCreateTime(hospitalByHoscode.getCreateTime());
            hospital.setUpdateTime(new Date());
            hospital.setIsDeleted(0);
            hospRepostitory.save(hospital);
        } else {
            //不存在添加
            hospital.setStatus(0);
            hospital.setCreateTime(new Date());
            hospital.setUpdateTime(new Date());
            hospital.setIsDeleted(0);
            hospRepostitory.save(hospital);
        }
    }

    @Override
    public Hospital getByHoscode(String hoscode) {
        Hospital hospital = hospRepostitory.getHospitalByHoscode(hoscode);
        return hospital;
    }

    @Override
    public Page<Hospital> selectHosPage(Integer page, Integer limit, HospitalQueryVo hospitalQueryVo) {
        //构建pageable
        Pageable pageable=PageRequest.of(page,limit);
        //构建example
        ExampleMatcher matcher = ExampleMatcher.matching().withStringMatcher(ExampleMatcher.StringMatcher.CONTAINING).withIgnoreCase(true);
        Hospital hospital = new Hospital();
        //进行对象转换
        BeanUtils.copyProperties(hospitalQueryVo,hospital);

        Example<Hospital> e = Example.of(hospital, matcher);

        Page<Hospital> all = hospRepostitory.findAll(e, pageable);

        //遍历list集合，进行医院等级封装
        all.getContent().stream().forEach(item->{
            this.setHospitalHosType(item);
        });
        return all;
    }

    private Hospital setHospitalHosType(Hospital item) {
        //根据dictcode和value获取医院等级名称
        String hostype = dictFeignClient.getName("Hostype", item.getHostype());
        String province = dictFeignClient.getName(item.getProvinceCode());
        String city = dictFeignClient.getName(item.getCityCode());
        String district = dictFeignClient.getName(item.getDistrictCode());

        item.getParam().put("fullAddress",province+city+district);
        item.getParam().put("hostypeString",hostype);
        return item;
    }
}
